<?php
/**
 * Name: AdminUserLogic.class.php
 * Description:
 * User: DIY
 * Date: 2016/9/13
 * Time: 16:35
 */

namespace Common\Logic;


use Common\Library\AuthEncrypt;
use Common\Model\AdminRoleModel;
use Common\Model\AdminUserModel;

class AdminUserLogic{

    private $AdminUserModel;

    public function __construct(){
        $this->AdminUserModel = new AdminUserModel();
    }
    /*
     * 用户登录
     * */
    public function Login($username,$password){
        if(empty($username)){
            return array('status'=>'96','msg'=>'登录用户名不能为空');
        }
        if(empty($password)){
            return array('status'=>'97','msg'=>'登录密码不能为空');
        }
        $admin_user = $this->AdminUserModel->getUserByName($username);
        if(empty($admin_user)){
            return array('status'=>'98','msg'=>'账户不存在');
        }
        //验证用户是否被禁用
        if($admin_user['status']){
            return array('status'=>'94','msg'=>'账户已被禁用');
        }
        $AdminRoleModel = new AdminRoleModel();
        $check_role = $AdminRoleModel->find($admin_user['role_id']);
        if($check_role['status']){
            return array('status'=>'93','msg'=>'角色已被禁用');
        }
        if($admin_user['password']==md5($password)){
            //更新数据
            $param['last_login_time'] = time();
            $param['last_login_ip'] = get_client_ip();
            $param['id'] = $admin_user['id'];
            $this->AdminUserModel->data($param)->save();
            //保存COOKIE加密信息
            $AuthEncrypt = new AuthEncrypt();
            //设置动态加密KEY和有效期
            $AuthEncrypt->setExpiry(C('AUTH_EXPIRE'));
            $AuthEncrypt->setKey(C('AUTH_KEY'));
            $user_id =  $AuthEncrypt->encode($admin_user['id']);
            cookie('admin_user',$user_id);
            return array('status'=>'100','msg'=>'登录成功','url'=>U('Admin/Index/index'));
        }
        else{
            return array('status'=>'99','msg'=>'密码错误');
        }
    }






}